package com.simon.nvwo.modules.web.servlet;



import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

/**
 * Servlet操作sesssion实例
 */
public class SessionExample extends HttpServlet{

    public SessionExample() {
        super();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //获取当前session
        HttpSession session=request.getSession();
        //session使用key-value存储数据
        session.setAttribute("loginTime",new Date());// 设置一个当前时间作为登录时间参数

        PrintWriter out = response.getWriter();
        // 获取Session属性
        out.println("登录时间为：" +(Date)session.getAttribute("loginTime"));
    }


}
